package com.hupu.jrs.ui

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.view.Window
import android.view.WindowManager
import com.hupu.jrs.AppManager
import com.hupu.jrs.MyApplication
import com.hupu.jrs.R
import com.hupu.jrs.injector.component.ActivityComponent
import com.hupu.jrs.injector.component.ApplicationComponent
import com.hupu.jrs.injector.module.ActivityModule
import com.hupu.jrs.util.ResourceUtil
import com.hupu.jrs.util.SettingPrefUtil
import com.hupu.jrs.util.StatusBarUtil
import com.hupu.jrs.util.ThemeUtil
import com.umeng.analytics.MobclickAgent

/**
 * Created by sll on 2016/3/9.
 */
abstract class BaseActivity : AppCompatActivity() {

    protected var mActivityComponent: ActivityComponent? = null

    protected val applicationComponent: ApplicationComponent?
        get() = (application as MyApplication).applicationComponent

    protected val activityModule: ActivityModule
        get() = ActivityModule(this)

    /**
     * is applyStatusBarTranslucency
     */
    protected abstract val isApplyStatusBarTranslucency: Boolean

    protected abstract val isApplyStatusBarColor: Boolean

    val statusBarHeight: Int
        get() = ResourceUtil.getStatusBarHeight(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        applicationComponent!!.inject(this)
        initTheme()
        super.onCreate(savedInstanceState)
        setContentView(initContentView())
        setTranslucentStatus(isApplyStatusBarTranslucency)
        setStatusBarColor(isApplyStatusBarColor)
        initInjector()
        initUiAndListener()
        AppManager.appManager.addActivity(this)
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

    private fun initTheme() {
        var theme: Int
        try {
            theme = packageManager.getActivityInfo(componentName, 0).theme
        } catch (e: PackageManager.NameNotFoundException) {
            return
        }

        if (theme != R.style.AppThemeLaunch) {
            theme = ThemeUtil.themeArr[SettingPrefUtil.getThemeIndex(this)][if (SettingPrefUtil.getNightModel(this)) 1 else 0]
        }
        setTheme(theme)
    }

    /**
     * 设置view
     */
    abstract fun initContentView(): Int

    /**
     * 注入Injector
     */
    abstract fun initInjector()

    /**
     * init UI && Listener
     */
    abstract fun initUiAndListener()

    /**
     * set status bar translucency
     */
    protected fun setTranslucentStatus(on: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val win = window
            val winParams = win.attributes
            val bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            if (on) {
                winParams.flags = winParams.flags or bits
            } else {
                winParams.flags = winParams.flags and bits.inv()
            }
            win.attributes = winParams
        }
    }

    /**
     * use SystemBarTintManager
     */
    fun setStatusBarColor(on: Boolean) {
        if (on) {
            StatusBarUtil.setColor(this, ResourceUtil.getThemeColor(this), 0)
        }
    }

    fun reload() {
        val intent = intent
        overridePendingTransition(0, 0)
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
        finish()
        overridePendingTransition(0, 0)
        startActivity(intent)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onDestroy() {
        super.onDestroy()
        AppManager.appManager.finishActivity(this)
    }
}
